<?php

/////////////////////////////////////////////////////////////////////////
// You must define the following constants:
// SALESFORCE_WSDL
// SALESFORCE_API_USERNAME
// SALESFORCE_API_PASSWORD
////////////////////////////////////////////////////////////////////////


ini_set("soap.wsdl_cache_enabled", "0");

require_once ('soapclient/SforceEnterpriseClient.php');

class SalesForceClient {
	
	private $soapClient;
	private $connection;
	private $login;
	private $debug;
	
	function __construct($debug = false) {

		$this->debug = $debug;
		
		try {
			
			$this->connection 				= new SforceEnterpriseClient();
			
			if ($this->connection) {
				
				$this->soapClient 		= $this->connection->createConnection(SALESFORCE_WSDL);
				$this->login 				= $this->connection->login(SALESFORCE_API_USERNAME, SALESFORCE_API_PASSWORD);
				
			} else { throw(new Exception('Could not create connection!')); }
			
		} catch (Exception $e) {
			
			error_log('SalesForce Error (' . __FUNCTION__ . '):' . $e->faultstring);
			if ($this->debug) { print "SalesForceException " . __FUNCTION__ . " : " . print_r($e, true) . "<br />\n"; }
			
		}
		
		
		
	}
	
	function checkLeadExists($email) {
		
		$success = FALSE;
		
		try {

			$query 			= "SELECT Email from Lead WHERE email='$email' Limit 1";
			$response 		= $this->connection->query($query);
	
			if ($response) {
				
				if ($response->records[0]->Email == $email) { $success = TRUE; }
				
			} else { throw(new Exception('Could not get a query response')); }

		} catch (Exception $e) {
			
			error_log('SalesForce Error (' . __FUNCTION__ . '):' . $e->faultstring);
			if ($this->debug) { print "SalesForceException " . __FUNCTION__ . " : " . print_r($e, true) . "<br />\n"; }
		
		}	
		
		return $success;
		
	}
	
	function getAccountID($email) {
		
		$success = null;
		
		try {

			$query 			= "SELECT Id from Account WHERE email='$email' Limit 1";
			$response 		= $this->connection->query($query);
	
			if ($response) {
				
				if ($response->records[0]->Id) { $success = $response->records[0]->Id; }
				
			} else { throw(new Exception('Could not get a query response')); }

		} catch (Exception $e) {
			
			error_log('SalesForce Error (' . __FUNCTION__ . '):' . $e->faultstring);
			if ($this->debug) { print "SalesForceException " . __FUNCTION__ . " : " . print_r($e, true) . "<br />\n"; }
			
		}	
		
		return $success;
		
	}
	
	function getLeadID($email) {
		
		$success = null;
		
		try {

			$query 			= "SELECT Id from Lead WHERE email='$email' Limit 1";
			$response 		= $this->connection->query($query);
	
			if ($response) {
				
				if ($response->records[0]->Id) { $success = $response->records[0]->Id; }
				
			} else { throw(new Exception('Could not get a query response')); }

		} catch (Exception $e) {
			
			error_log('SalesForce Error (' . __FUNCTION__ . '):' . $e->faultstring);
			if ($this->debug) { print "SalesForceException " . __FUNCTION__ . " : " . print_r($e, true) . "<br />\n"; }
			
		}	
		
		return $success;
		
	}
	
	function updateAccountSingle($customerID, $fieldName, $fieldValue) {
		
		$success = FALSE;

		$accountID = $this->getAccountID($customerID);
			
		if ($accountID) {
		
			try {
	
				$sObject = new stdClass();
				$sObject->Id = $accountID;
				$sObject->{$fieldName} = $fieldValue;
				
	          	$response = $this->connection->update(array($sObject), 'Account');
		
				if ($response) {
					
					if (!$response->errors) { $success = TRUE; }
					 
				} else { throw(new Exception('Could not get a query response')); }
	
			} catch (Exception $e) {
				
				error_log('SalesForce Error (' . __FUNCTION__ . '):' . $e->faultstring);
				if ($this->debug) { print "SalesForceException " . __FUNCTION__ . " : " . print_r($e, true) . "<br />\n"; }
				
			}	
		
		}
		
		return $success;
		
	}
	
}


?>